package isp.apps.example.shoppingcart.persistence.hibernate;

import isp.apps.example.shoppingcart.domain.CartItem;

import isp.apps.example.shoppingcart.persistence.CartItemsRepository;

import isp.apps.example.shoppingcart.persistence.PersistenceManagerFactory;

import isp.utils.cjis.persistence.PersistenceException;
import isp.utils.cjis.persistence.PersistenceManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HibernateCartItemsRepository
  implements CartItemsRepository
{

  private PersistenceManager persistenceManager = PersistenceManagerFactory
    .getPersistenceManager();

  private static final Log log =
    LogFactory.getLog(HibernateCartItemsRepository.class);

  public HibernateCartItemsRepository()
  {
  }

  public void saveCartItem(CartItem ci)
  {
    try
    {
      if (ci.getId() == null)
        persistenceManager.save(ci);
      else
        persistenceManager.update(ci);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
    }
  }

  public void deleteCartItem(CartItem ci)
  {
    try
    {
      persistenceManager.delete(ci);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
    }
  }
}
